package com.ll.daipao.config;

import com.ll.daipao.common.Constant;
import com.ll.daipao.dto.Manger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by ll on 2017/9/24.
 */
public class LoginFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        Object attribute = request.getSession().getAttribute(Constant.LOGIN_USER);
        if(attribute!=null){
            filterChain.doFilter(request, response);
            return;
        }
        // 获得当前请求的URI
        String nowPath = request.getRequestURI();
        if(request.getSession().getAttribute(Constant.LOGIN_USER)==null){
            if("/login.do".equals(nowPath)||"/loginSub.do".equals(nowPath)){
                filterChain.doFilter(request, response);
                return;
            }else {
                response.sendRedirect(request.getContextPath() + "/login.jsp");
                return;
            }
        }
        filterChain.doFilter(request, response);
    }

    public void destroy() {

    }
}
